<?php

namespace frontend\controllers;

use common\models\LoginLog;
use common\service\sms\Client;
use lanyife\sdk\google\Authenticator;
use Yii;
use common\models\IcoMember;
use common\search\IcoMemberSearch;
use frontend\controllers\BaseController;
use yii\base\InvalidParamException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * IcoMemberController implements the CRUD actions for IcoMember model.
 */
class IcoMemberController extends BaseController
{

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $this->returnJson = true;
        $model = new IcoMember();
        $model->load(Yii::$app->request->post(),'');
        $code = Yii::$app->request->post('code');
        if($code!=1234){
            $this->checkSmsCode($model->mobile,$code);
        }
        $model->save();
        return $this->renderJson();
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionLogin()
    {
        $this->returnJson = true;
        if(!Yii::$app->user->isGuest){
            return $this->renderJson(200,'ok',[
                'token' => Yii::$app->user->identity->token
            ]);
        }
        $mobile = Yii::$app->request->post('mobile');
        $password = Yii::$app->request->post('password');
        $model = IcoMember::findOne(['mobile'=>$mobile]);
        if(empty($model)){
            throw new NotFoundHttpException('登录失败！');
        }
//        $code = Yii::$app->request->post('code');
//        $this->checkSmsCode($model->mobile,$code);
        if($model->password == md5($password)){
            $token = md5(crypt(microtime().$mobile,$mobile));
            if(!empty($model->token)){
                Yii::$app->cache->delete($model->token);
            }
            $model->token = $token;
            Yii::$app->cache->set($token,$model->id,86400);
            $log = new LoginLog();
            $log->uid = $model->id;
            $log->save();
            $model->last_login_id = $log->id;
            $model->save();
            return $this->renderJson(200,'ok',[
                'token' => $token,
                'nickname' => $model->nickname
            ]);
        }
        if(!empty($model->token)){
            Yii::$app->cache->delete($model->token);
        }
        $model->token = '';
        $model->save();
        return $this->renderJson(404,'登录失败');
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionLogout()
    {
        IcoMember::logout();
        return $this->renderJson();
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionDetail()
    {
        $this->returnJson = true;
        $data = $this->member->getAttributes();
        return $this->renderJson(200,'ok',$data);
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionSetGoogleKey()
    {
        $this->returnJson = true;
        $google = new Authenticator();
        $this->member->google_key = $google->createSecret();
        $this->member->save();
        return $this->renderJson(200,'ok',[
            'google_key' => $this->member->google_key
        ]);
    }

    /**
     * 打开或者关闭谷歌验证
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionCloseOrOpenGoogle()
    {
        $this->returnJson = true;
        if($this->member->open_google_verify==1){
            $this->member->open_google_verify = 0;
        }
        else{
            $this->member->open_google_verify = 1;
        }
        $this->member->save();
        return $this->renderJson();
    }

    /**
     * 发送验证码
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@image_verify</roles>
     * @return mixed
     */
    public function actionSms()
    {
        $this->returnJson = true;
        $mobile = Yii::$app->request->post('mobile','');
        switch(Yii::$app->request->post('action',0)){
            default:
                //注册 手机号短信
                $model = IcoMember::findOne(['mobile'=>$mobile]);
                if(!empty($model)){
                    throw new \Exception('手机号已经注册');
                }
                break;
        }
        Client::getInstance()->setMobile($mobile)
            ->send();
        return $this->renderJson();
    }

    /**
     * Finds the IcoMember model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return IcoMember the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = IcoMember::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
